package com.example.util;

import com.example.bean.Student1;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;

/**
 * @program: sms
 * @description:
 * @author: zhanghz001
 * @create: 2020-11-02 14:18
 **/
public class RequestServletUtil extends HttpServlet {

    protected void request(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取各种路径
        setEncoding(req, resp);
        path(req, resp);
        getHeader(req, resp);
        getObject(req, resp);
    }

    private void setEncoding(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        //获取请求正文，会有乱码问题。
        //  是在获取的时候就已经乱码了
        req.setCharacterEncoding("UTF-8");
    }

    private void getObject(HttpServletRequest req, HttpServletResponse resp) {
        try {
            Student1 student = new Student1();
            BeanUtils.populate(student, req.getParameterMap());
            System.out.println(student.toString());
            System.out.println("test2");

        } catch (Exception e) {
            e.printStackTrace();
        } finally {

        }
    }

    protected void getHeader(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.根据请求头名称获取一个值
        String connection = req.getHeader("connection");
        System.out.println(connection);
        System.out.println("--------------");

        //2.根据请求头名称获取多个值
        Enumeration<String> values = req.getHeaders("accept-encoding");
        while (values.hasMoreElements()) {
            String value = values.nextElement();
            System.out.println(value);
        }
        System.out.println("--------------");

        //3.获取所有的请求头名称
        Enumeration<String> names = req.getHeaderNames();
        while (names.hasMoreElements()) {
            String name = names.nextElement();
            String value = req.getHeader(name);
            System.out.println(name + "," + value);
        }

    }

    void path(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取虚拟目录名称 getContextPath()
        String contextPath = req.getContextPath();
        System.out.println(contextPath);

        //2.获取Servlet映射路径 getServletPath()
        String servletPath = req.getServletPath();
        System.out.println(servletPath);

        //3.获取访问者ip getRemoteAddr()
        String ip = req.getRemoteAddr();
        System.out.println(ip);

        //4.获取请求消息的数据 getQueryString()
        String queryString = req.getQueryString();
        System.out.println(queryString);

        //5.获取统一资源标识符 getRequestURI()    /request/servletDemo01   共和国
        String requestURI = req.getRequestURI();
        System.out.println(requestURI);

        //6.获取统一资源定位符 getRequestURL()    http://localhost:8080/request/servletDemo01  中华人民共和国
        StringBuffer requestURL = req.getRequestURL();
        System.out.println(requestURL);

    }

}
